from django.forms import Form, CharField, EmailField, TextInput, widgets, IntegerField
from django.core.exceptions import ValidationError
import re



def mobile_validate(value):
    print('{:-^60}'.format('手机号码格式错误'))
    mobile_re = re.compile(r'^(13[0-9]|15[012356789]|17[678]|18[0-9]|14[57])[0-9]{8}$')
    if not mobile_re.match(value):
        raise ValidationError('手机号码格式错误')

def valueIsNullAndEmpty(value):
    print('{:-^60}'.format('valueIsNullAndEmpty'))
    print(value)
    print(str(value).isspace())
    if str(value).isspace():
        return ValidationError('不能为空')

user_type_choice = (
    (0, '请选择'),
    (1, '普通用户'),
    (2, '高级用户'),
)

class UserInfoForm(Form):
    usertype = IntegerField(widget = widgets.Select(choices = user_type_choice))
    #username = forms.CharField(error_messages={'required': u'用户名不能为空'})
    username = CharField(required = False, error_messages={'required': u'用户名不能为空'})
    password = CharField(required = False, error_messages={'required': u'密码不能为空'})
    mobile = CharField(validators = {
        valueIsNullAndEmpty,
        mobile_validate
    }, error_messages = {
        'required': u'手机号不能为空'
    },  widget = TextInput(attrs = { 
        'class': "form-control",
        'placeholder': u'手机号码',
        'data-id': '123123123'
    }), required = False)

    email = EmailField(required = False, error_messages={'required': u'邮箱不能为空'})





